import request from './axios';

export interface IShipmentDto {
  id?: number;
  trackingNumber: string;
  orderId: string;
  shippingMethod: string;
  carrierName: string;
  customerId?: string;
  status: ShipmentStatus;
  shippingDate?: string;
  estimatedDeliveryDate?: string;
  actualDeliveryDate?: string;
  originAddress: string;
  destinationAddress: string;
  packageDimensions?: string;
  packageWeight?: number;
  shippingLabelUrl?: string;
  notes?: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface IShipmentRequest {
  orderId: string;
  shippingMethod: string;
  carrierName: string;
  customerId?: string;
  estimatedDeliveryDate?: string;
  originAddress: string;
  destinationAddress: string;
  packageDimensions?: string;
  packageWeight?: number;
  notes?: string;
}

export interface ICarrierDto {
  id?: number;
  name: string;
  code?: string;
  contactPerson?: string;
  contactEmail?: string;
  contactPhone?: string;
  trackingUrlTemplate?: string;
  apiCredentials?: string;
  isActive: boolean;
  supportedCountries?: string;
  serviceOptions?: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface ICarrierRequest {
  name: string;
  code?: string;
  contactPerson?: string;
  contactEmail?: string;
  contactPhone?: string;
  trackingUrlTemplate?: string;
  apiCredentials?: string;
  isActive?: boolean;
  supportedCountries?: string;
  serviceOptions?: string;
}

export enum ShipmentStatus {
  PENDING = 'PENDING',
  PROCESSING = 'PROCESSING',
  SHIPPED = 'SHIPPED',
  IN_TRANSIT = 'IN_TRANSIT',
  OUT_FOR_DELIVERY = 'OUT_FOR_DELIVERY',
  DELIVERED = 'DELIVERED',
  EXCEPTION = 'EXCEPTION',
  RETURNED = 'RETURNED',
  CANCELLED = 'CANCELLED'
}

const logisticsApi = {
  // 物流单相关接口
  // 获取所有物流单
  getAllShipments: () => {
    return request({
      url: '/shipment',
      method: 'get'
    });
  },
  
  // 根据ID获取物流单
  getShipmentById: (id: number) => {
    return request({
      url: `/shipment/${id}`,
      method: 'get'
    });
  },
  
  // 根据物流单号获取物流单
  getShipmentByTrackingNumber: (trackingNumber: string) => {
    return request({
      url: `/shipment/tracking/${trackingNumber}`,
      method: 'get'
    });
  },
  
  // 根据订单ID获取物流单
  getShipmentsByOrderId: (orderId: string) => {
    return request({
      url: `/shipment/order/${orderId}`,
      method: 'get'
    });
  },
  
  // 根据客户ID获取物流单
  getShipmentsByCustomerId: (customerId: string) => {
    return request({
      url: `/shipment/customer/${customerId}`,
      method: 'get'
    });
  },
  
  // 根据状态获取物流单
  getShipmentsByStatus: (status: ShipmentStatus) => {
    return request({
      url: `/shipment/status/${status}`,
      method: 'get'
    });
  },
  
  // 根据承运商获取物流单
  getShipmentsByCarrierName: (carrierName: string) => {
    return request({
      url: `/shipment/carrier/${carrierName}`,
      method: 'get'
    });
  },
  
  // 创建物流单
  createShipment: (data: IShipmentRequest) => {
    return request({
      url: '/shipment',
      method: 'post',
      data
    });
  },
  
  // 更新物流单状态
  updateShipmentStatus: (id: number, status: ShipmentStatus) => {
    return request({
      url: `/shipment/${id}/status`,
      method: 'put',
      params: { status }
    });
  },
  
  // 更新物流单号
  updateShipmentTracking: (id: number, trackingNumber: string) => {
    return request({
      url: `/shipment/${id}/tracking`,
      method: 'put',
      params: { trackingNumber }
    });
  },
  
  // 更新预计配送日期
  updateShipmentDeliveryDate: (id: number, estimatedDeliveryDate: string) => {
    return request({
      url: `/shipment/${id}/delivery-date`,
      method: 'put',
      data: { estimatedDeliveryDate }
    });
  },
  
  // 标记为已发货
  markShipmentAsShipped: (id: number, shippingDate?: string) => {
    return request({
      url: `/shipment/${id}/ship`,
      method: 'put',
      data: shippingDate ? { shippingDate } : {}
    });
  },
  
  // 标记为已送达
  markShipmentAsDelivered: (id: number, deliveryDate?: string) => {
    return request({
      url: `/shipment/${id}/deliver`,
      method: 'put',
      data: deliveryDate ? { deliveryDate } : {}
    });
  },
  
  // 删除物流单
  deleteShipment: (id: number) => {
    return request({
      url: `/shipment/${id}`,
      method: 'delete'
    });
  },
  
  // 承运商相关接口
  // 获取所有承运商
  getAllCarriers: () => {
    return request({
      url: '/carrier',
      method: 'get'
    });
  },
  
  // 获取启用的承运商
  getActiveCarriers: () => {
    return request({
      url: '/carrier/active',
      method: 'get'
    });
  },
  
  // 根据ID获取承运商
  getCarrierById: (id: number) => {
    return request({
      url: `/carrier/${id}`,
      method: 'get'
    });
  },
  
  // 根据名称获取承运商
  getCarrierByName: (name: string) => {
    return request({
      url: `/carrier/name/${name}`,
      method: 'get'
    });
  },
  
  // 根据代码获取承运商
  getCarrierByCode: (code: string) => {
    return request({
      url: `/carrier/code/${code}`,
      method: 'get'
    });
  },
  
  // 创建承运商
  createCarrier: (data: ICarrierRequest) => {
    return request({
      url: '/carrier',
      method: 'post',
      data
    });
  },
  
  // 更新承运商
  updateCarrier: (id: number, data: ICarrierRequest) => {
    return request({
      url: `/carrier/${id}`,
      method: 'put',
      data
    });
  },
  
  // 切换承运商状态
  toggleCarrierStatus: (id: number, isActive: boolean) => {
    return request({
      url: `/carrier/${id}/status`,
      method: 'put',
      params: { isActive }
    });
  },
  
  // 删除承运商
  deleteCarrier: (id: number) => {
    return request({
      url: `/carrier/${id}`,
      method: 'delete'
    });
  }
};

export default logisticsApi; 